<?php

use yii\helpers\StringHelper;

/* @var $this yii\web\View */
/* @var $generator backend\modules\gii\generators\crud\Generator */
/* @var $tableName string full table name */
/* @var $className string class name */
/* @var $queryClassName string query class name */
/* @var $tableSchema yii\db\TableSchema */
/* @var $labels string[] list of attribute labels (name => label) */
/* @var $rules string[] list of validation rules */
/* @var $relations array list of relations (name => relation declaration) */

$modelClass = StringHelper::basename($generator->modelClass);
$searchModelClass = StringHelper::basename($generator->searchModelClass);
$formModelClass = StringHelper::basename($generator->formModelClass);

echo "<?php\n";
?>

namespace <?= StringHelper::dirname(ltrim($generator->formModelClass, '\\')) ?>;

use common\widgets\ActiveForm;
use common\widgets\Form;
use Yii;
use yii\helpers\ArrayHelper;
use yii\web\ServerErrorHttpException;

/**
 * This is the form model class for model "<?php echo $modelClass ?>".
 */
class <?= $formModelClass ?> extends <?= '\\' . ltrim($generator->modelClass, '\\') . "\n" ?>
{

    /**
     * @inheritdoc
     */
    public $isNewRecord;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return parent::rules();
    }

    const SCENARIO_CREATE = 'admin-create';
    const SCENARIO_UPDATE = 'admin-update';

    /**
     * @inheritdoc
     */
    public function scenarios()
    {
        return [
            'admin-create' => ['<?php echo implode("', '", $generator->getColumnNames()) ?>'],
            'admin-update' => ['<?php echo implode("', '", $generator->getColumnNames()) ?>'],
        ];
    }

    /**
     * 返回当前表单的名称
     *
     * @return string
     */
    public function classLabelName()
    {
        return <?php echo $generator->generateString($modelClass) ?>;
    }

    /**
     * 列表定义
     *
     * @param ActiveForm|Form $form
     * @param $model <?php echo $formModelClass . "\n" ?>
     * @param mixed $extra
     * @return array
     * @throws \yii\web\ServerErrorHttpException
     */
    public function formRows(&$form, $model, $extra)
    {
        return [
            [
                'box' => ['title' => "基础信息"],
                'columns' => 2,
                'attributes' => [
                    <?php foreach ($generator->getColumnNames() as $name) { ?>
                        <?php

                        echo "\n";

                        if ($name == 'id')
                        {
                            ?>
                    '<?php echo $name ?>' => [
                        'type' => Form::INPUT_TEXT,
                        'options' => ['disabled' => true],
                    ],
                            <?php
                        }
                        elseif ($name == 'password')
                        {
                            ?>
                    '<?php echo $name ?>' => [
                        'type' => Form::INPUT_PASSWORD
                    ],
                            <?php
                        }
                        else
                        {
                            ?>
                    '<?php echo $name ?>' => [
                        'type' => Form::INPUT_TEXT
                    ],
                            <?php
                        }
                        ?>
                    <?php } echo "\n"; ?>
                ],
            ],
        ];
    }

}
